import jsonp from "jsonp";
import { Toast } from "vant";

import { getMccDetail } from "@/api/mcc";
import { getAreatext } from "@/api/area";
import { getSystemRate, getUperAgentRate } from "@/api/rate.js";

const state = () => {
  return {
    //  基本信息
    openType: "", //分店类型
    openTypeText: "",
    merchantId: "",
    merchantText: "",
    merchantType: "", //商户类型
    merchantTypeText: "",
    mcc: "",
    mccText: "",
    bizStartTime: "",
    bizEndTime: "",
    legalStartTime: "",
    accountStartTime: "",
    legalEndTime: "",
    accountEndTime: "",
    bizAreaId: [],
    bizAreaText: "",
    defaultChannelCode: "",
    channelCode: "",
    person: "",

    merchantFullName: "",
    merchantAbbrName: "",
    bizLicense: "",
    merchantAddress: "", //执照地址//商户地址
    bizScope: "",
    legalName: "",
    accountUserName: "",
    accountName: "",
    legalIdcard: "",
    legalIdcardAddress: "", // 法人身份证地址
    accountIdcard: "",
    bizImg: "", //营业执照
    legalFontImg: "", //法人身份证正面
    legalBackImg: "", //法人身份证反面
    legalHandFontImg: "", //法人手持身份证照
    accountFontImg: "", //结算人身份证正面
    accountBackImg: "", //结算人身份证反面
    is_step1: true,
    no_step1: false,
    //附件信息
    storeAddressImg: "", //营业场所照片
    storeDoorImg: "", //门头照
    cashierImg: "",
    proofHandImg: "", //法人手持商户关系证明
    proofImg: "",
    assistImg: [],
    is_step4: true,
    no_step4: false,

    //费率信息
    payType: "",
    payTypeText: "",
    //结算周期
    firstSettleTypeText: "",
    firstSettleType: "",
    //定时结算时间
    firstSettleTimeText: "",
    firstSettleTime: "",
    equipmentSN: "",
    rate_sk: "",
    rate_fd: "",
    rate_sm: "",
    rate_d0: "",
    rate_d1: "",
    is_step3: true,
    no_step3: false,

    //  电子签名
    signature: "",
    longitude: "",
    latitude: "",

    autograph: "",
    is_account: true,
    no_account: false,

    syetem_do_minValue: "",
    agent_do_minValue: "",
    rate_d0_minValue: "",
    rate_d0_defaultValue: "",
    rate_d0_maxValue: "",

    system_sk_minValue: "",
    agent_sk_minValue: "",
    rate_sk_minValue: "",
    rate_sk_defaultValue: "",
    rate_sk_maxValue: "",

    system_sm_minValue: "",
    agent_sm_minValue: "",
    rate_sm_minValue: "",
    rate_sm_defaultValue: "",
    rate_sm_maxValue: "",

    system_fd_minValue: "",
    agent_fd_minValue: "",
    rate_fd_minValue: "",
    rate_fd_defaultValue: "",
    rate_fd_maxValue: "",

    system_d1_minValue: "",
    agent_d1_minValue: "",
    rate_d1_minValue: "",
    rate_d1_defaultValue: "",
    rate_d1_maxValue: "",

    system_ds_minValue: "",
    agent_ds_minValue: "",
    rate_ds_minValue: "",
    rate_ds_defaultValue: "",
    rate_ds_maxValue: "",

    sk_promoteBusiness: "",
    sk_allowShow: "",
    do_promoteBusiness: "",
    do_allowShow: "",
    sm_promoteBusiness: "",
    sm_allowShow: "",
    fd_promoteBusiness: "",
    fd_allowShow: "",

    d1_allowShow: "",
    d1_promoteBusiness: "",
    ds_allowShow: "",
    ds_promoteBusiness: "",
    isComplete: 0,
    acquiringManager:'',
    acquiringPeople:'',
    acquiringRemarks:'',
  };
};

const mutations = {
  //  基本信息
  setOpenType(state, data) {
    state.openType = data.value;
    state.openTypeText = data.text;
  },
  setMerchant(state, data) {
    state.merchantId = data.id;
    state.merchantText = data.merchantFullName;
  },
  setMerchantType(state, data) {
    state.merchantType = data.value;
    state.merchantTypeText = data.text;
  },
  setMcc(state, data) {
    state.mcc = data.code;
    state.mccText = data.name;
  },
  setLocation(state, data) {
    state.bizAreaText = data.poiaddress;
  },
  setBizStartTime(state, data) {
    state.bizStartTime = data;
  },
  setBizStartEnd(state, data) {
    state.bizEndTime = data;
  },
  setDefaultChannelCode(state, data) {
    state.defaultChannelCode = data;
  },
  setLegalStartTime(state, data) {
    state.legalStartTime = data;
  },
  setAccountStartTime(state, data) {
    state.accountStartTime = data;
  },
  setLegalStartEnd(state, data) {
    state.legalEndTime = data;
  },
  setAccountStartEnd(state, data) {
    state.accountEndTime = data;
  },
  setBizArea(state, data) {
    state.bizAreaId = data.bizAreaId;
    state.bizAreaText = data.bizAreaText;
  },
  ISetMerchantFullName(state, data) {
    state.merchantFullName = data;
  },
  ISetMerchantAbbrName(state, data) {
    state.merchantAbbrName = data;
  },
  ISetBizLicense(state, data) {
    state.bizLicense = data;
  },
  ISetMerchantAddress(state, data) {
    state.merchantAddress = data;
  },
  ISetBizScope(state, data) {
    state.bizScope = data;
  },
  ISetLegalName(state, data) {
    state.legalName = data;
  },
  ISetAccountUserName(state, data) {
    state.accountUserName = data;
  },
  ISetAccountName(state, data) {
    state.accountName = data;
  },
  ISetAccountUserName(state, data) {
    state.accountUserName = data;
  },
  ISetLegalIdcard(state, data) {
    state.legalIdcard = data;
  },
  ISetLegalIdcardAddress(state, data) {
    state.legalIdcardAddress = data;
  },
  ISetAccountIdcard(state, data) {
    state.accountIdcard = data;
  },
  PSetBizImg(state, url) {
    state.bizImg = url;
  },
  PSetPerson(state, data) {
    state.person = data;
  },
  PSetLegalFontImg(state, url) {
    state.legalFontImg = url;
  },
  PSetLegalHandFontImg(state, url) {
    state.legalHandFontImg = url;
  },
  PSetAccountFontImg(state, url) {
    state.accountFontImg = url;
  },
  PSetLegalBackImg(state, url) {
    state.legalBackImg = url;
  },

  PSetAccountBackImg(state, url) {
    state.accountBackImg = url;
  },
  //附件信息
  PSetStoreAddressImg(state, url) {
    state.storeAddressImg = url;
  },
  PSetStoreDoorImg(state, url) {
    state.storeDoorImg = url;
  },
  PSetCashierImg(state, url) {
    state.cashierImg = url;
  },
  PSetProofHandImg(state, url) {
    state.proofHandImg = url;
  },
  PSetProofImg(state, url) {
    state.proofImg = url;
  },
  PSetAssistImg(state, url) {
    state.assistImg.push({
      url,
      link: true,
    });
  },
  setAssistImg(state, arr) {
    state.assistImg = arr.map((item) => {
      return {
        url: item,
        link: true,
      };
    });
  },
  PRemoveAssistImg(state, index) {
    state.assistImg.splice(index, 1);
  },
  //费率信息
  setPayType(state, data) {
    state.payType = data.value;
    state.payTypeText = data.text;
  },
  //定时结算时间
  setFirstSettleTime(state, data) {
    state.firstSettleTime = data.value;
    state.firstSettleTimeText = data.text;
  },
  //结算周期
  firstSettleType(state, data) {
    state.firstSettleTypeText = data.text;
    state.firstSettleType = data.value;
    state.entryChannelCode = data.channelCode;
    state.entryChannelId = data.channelId;
  },
  setChannelCode(state, data) {
    state.channelCode = data;
  },
  setEquipmentSN(state, data) {
    if (/^[0-9a-zA-Z]{0,255}$/.test(data)) {
      state.equipmentSN = data;
    } else {
      state.equipmentSN = "";
    }
  },
  setRate_sk(state, data) {
    if (
      Number(data) >= Number(state.rate_sk_minValue) &&
      Number(data) <= Number(state.rate_sk_maxValue) &&
      /^(\d+|\d+\.\d{1,2})$/.test(Number(data))
    ) {
      state.rate_sk = Number(data);
    } else {
      state.rate_sk = "";
    }
  },
  setRate_fd(state, data) {
    if (
      Number(data) >= Number(state.rate_fd_minValue) &&
      Number(data) <= Number(state.rate_fd_maxValue) &&
      /^[1-9]\d*$/.test(Number(data))
    ) {
      state.rate_fd = Number(data);
    } else {
      state.rate_fd = "";
    }
  },
  setRate_sm(state, data) {
    if (
      data.value >= data.minValue &&
      data.value <= data.maxValue &&
      /^(\d+|\d+\.\d{1,2})$/.test(data.value)
    ) {
      state.rate_sm = data.value;
    } else {
      state.rate_sm = "";
    }
  },
  setRate_d0(state, data) {
    if (
      data.value >= data.minValue &&
      data.value <= data.maxValue &&
      /^(\d+|\d+\.\d{1,2})$/.test(data.value)
    ) {
      state.rate_d0 = data.value;
    } else {
      state.rate_d0 = "";
    }
  },
  setRate_d1(state, data) {
    if (
      data.value >= data.minValue &&
      data.value <= data.maxValue &&
      /^(\d+|\d+\.\d{1,2})$/.test(data.value)
    ) {
      state.rate_d1 = data.value;
    } else {
      state.rate_d1 = data.value;
    }
  },
  set_rate_system(state, data) {
    data.forEach((i) => {
      i.defaultValue = i.defaultValue || 0;
      switch (i.rateType) {
        case 1:
          state.rate_sk_maxValue = i.maxValue;
          state.rate_sk_defaultValue = i.defaultValue;
          state.system_sk_minValue = i.minValue;
          state.sk_promoteBusiness = i.promoteBusiness;
          state.sk_allowShow = i.allowShow;
          break;
        case 2:
          state.rate_d0_maxValue = i.maxValue;
          state.rate_d0_defaultValue = i.defaultValue;
          state.rate_d0_minValue = i.minValue;
          state.do_promoteBusiness = i.promoteBusiness;
          state.do_allowShow = i.allowShow;
          break;
        case 3:
          state.rate_sm_maxValue = i.maxValue;
          state.rate_sm_defaultValue = i.defaultValue;
          state.system_sm_minValue = i.minValue;
          state.sm_promoteBusiness = i.promoteBusiness;
          state.sm_allowShow = i.allowShow;
          break;
        case 4:
          state.rate_fd_maxValue = i.maxValue;
          state.rate_fd_defaultValue = i.defaultValue;
          state.system_fd_minValue = i.minValue;
          state.fd_promoteBusiness = i.promoteBusiness;
          state.fd_allowShow = i.allowShow;
          break;
        case 5:
          state.rate_d1_maxValue = i.maxValue;
          state.rate_d1_minValue = i.minValue;
          state.rate_d1_defaultValue = i.defaultValue;
          state.system_d1_minValue = i.minValue;
          state.d1_promoteBusiness = i.promoteBusiness;
          state.d1_allowShow = i.allowShow;
          break;
        case 6:
          state.rate_ds_maxValue = i.maxValue;
          state.rate_ds_minValue = i.minValue;
          state.rate_ds_defaultValue = i.defaultValue;
          state.system_ds_minValue = i.minValue;
          state.ds_promoteBusiness = i.promoteBusiness;
          state.ds_allowShow = i.allowShow;
          break;
      }
    });
  },
  set_rate_agent(state, data) {
    data.forEach((i) => {
      switch (i.rateType) {
        case 2:
          state.agent_sk_minValue = i.rateValue;
          break;
        case 4:
          // state.rate_d0_minValue = i.rateValue;
          break;
        case 1:
          state.agent_sm_minValue = i.rateValue;
          break;
        case 3:
          state.agent_fd_minValue = i.rateValue;
      }
    });
  },
  //  电子签名
  setSignature(state, data) {
    state.signature = data;
  },
  //客户签名
  setAutograph(state, data) {
    state.autograph = data;
  },
  //  经纬度
  getLangLatitude(state, form) {
    let encodeURl = window.encodeURIComponent(
      `${state.bizAreaText}${state.merchantAddress}`
    );
    jsonp(
      `https://api.map.baidu.com/geocoding/v3/?address=${encodeURl}&output=json&ak=CreLUuVyX9qeCtwxC8E3twp46OfbrGWL&ret_coordtype=gcj02ll&callback=showLocation`,
      null,
      (err, data) => {
        if (data.status == 0) {
          state.longitude = data.result.location.lng;
          state.latitude = data.result.location.lat;
        } else {
          Toast(`无法获取该地址信息，请检查详细地址！`);
        }
      }
    );
  },
  setStep1(state, data) {
    let openTypeObj = {
      "1": "直营连锁门店",
      "2": "独立经营门店",
    };
    let storeTypeObj = {
      "1": "企业",
      "2": "个体",
      "3": "小微",
    };
    let transactionTypeObj = {
      "1": "扫码",
      "2": "扫码+刷卡",
    };

    if (data.storeNature && data.storeNature != "0") {
      state.openType = data.storeNature;
      state.openTypeText = openTypeObj[String(data.storeNature)];
    }
    if (data.storeType && data.storeType != "0") {
      state.merchantType = data.storeType;
      state.merchantTypeText = storeTypeObj[String(data.storeType)];
    }
    state.merchantFullName = data.storeFullName;
    state.merchantAbbrName = data.storeAbbrName;
    state.bizLicense = data.bizLicense;
    if (data.bizLicenseExp) {
      state.bizStartTime = data.bizLicenseExp.split(",")[0];
      state.bizEndTime = data.bizLicenseExp.split(",")[1];
    }
    if (data.storeProvince) {
      state.bizAreaId = [
        data.storeProvince,
        data.storeCity,
        data.storeDistrict,
      ];
    }
    state.merchantAddress = data.storeAddress;
    state.bizScope = data.bizScope;
    state.legalName = data.legalName;
    state.legalIdcard = data.legalIdcard;
    state.legalIdcardAddress = data.legalIdcardAddress;

    if (data.legalIdcardExp) {
      state.legalStartTime = data.legalIdcardExp.split(",")[0];
      state.legalEndTime = data.legalIdcardExp.split(",")[1];
    }
    if (data.transactionType && data.transactionType != "0") {
      state.payType = data.transactionType;
      state.payTypeText = transactionTypeObj[String(data.transactionType)];
    }
    state.assistImg = [];
    data.storeAccountAttach.forEach((i) => {
      switch (i.fileType) {
        case "1":
          state.legalFontImg = i.filePath;
          break;
        case "2":
          state.legalBackImg = i.filePath;
          break;
        case "8":
          state.legalHandFontImg = i.filePath;
          break;
        case "4":
          state.storeAddressImg = i.filePath;
          break;
        case "12":
          state.storeDoorImg = i.filePath;
          break;
        case "3":
          state.bizImg = i.filePath;
          break;
        case "19":
          state.proofImg = i.filePath;
          break;
        case "24":
          state.cashierImg = i.filePath;
          break;
        case "29":
          state.proofHandImg = i.filePath;
          break;
      }
      if (["14", "15", "16", "17", "18"].includes(i.fileType)) {
        state.assistImg.push({
          link: true,
          url: i.filePath,
        });
      }
    });
    // mcc
    if (data.mcc) {
      state.mcc = data.mcc;
      getMccDetail(data.mcc).then((res2) => {
        if (res2.code == 200) {
          state.mccText = res2.data.name;
        }
      });
    }
    // 执照省市
    if (data.storeProvince) {
      getAreatext({
        provinceCode: data.storeProvince,
        cityCode: data.storeCity,
        districtCode: data.storeDistrict,
      }).then((res3) => {
        if (res3.code == 200) {
          const { province } = res3.data;
          const { city } = res3.data;
          const { district } = res3.data;
          state.bizAreaText = province + city + district;
        }
      });
    }
    const settleTypeHandler = {
      1: "笔笔到",
      2: "D1",
      3: "T1",
      4: "定时结算",
      6: "定点结算",
    };
    const firstSettleType = [
      settleTypeHandler[data.firstSettleType],
      data.defaultChannelCode || data.entryChannelCode,
    ].filter((item) => item);
    state.firstSettleTypeText =
      firstSettleType.length === 2 ? firstSettleType.join("-") : "";
    state.firstSettleType = data.firstSettleType;
    state.firstSettleTimeText = data.firstSettleTime + ':00';
    state.firstSettleTime = data.firstSettleTime;
    // 新增渠道相关字段
    state.entryChannelId = data.defaultChannelId || data.entryChannelId;
    state.entryChannelCode = data.defaultChannelCode || data.entryChannelCode;
    // 补充商户ID
    console.log(firstSettleType, "firstSettleType", data);
  },
  setStep3(state, data) {},
  setStep4(state, data) {},
  setComplete(state, data) {
    state.isComplete = data;
  },
  // 推荐信息相关
  setAcquiring(state, data) {
    state.acquiringManager = data.acquiringManager || ''
    state.acquiringPeople = data.acquiringPeople || ''
    state.acquiringRemarks = data.acquiringRemarks || ''
  },
};

const actions = {
  getSystemRates(context) {
    getSystemRate().then((res) => {
      if (res.code == 200) {
        const { data } = res;
        context.commit("set_rate_system", data);
      }
    });
  },
  getUperAgentRates(context, agentId) {
    getUperAgentRate(agentId).then((res) => {
      if (res.code == 200) {
        const { data } = res;
        context.commit("set_rate_agent", data);
      }
    });
  },
};

export default {
  namespace: true,
  state,
  mutations,
  actions,
};
